﻿using System;
using System.Linq;
using System.Reflection;
using FluentAssertions;
using LINQPad.OpenAccess.Metadata.Reflection;
using LINQPad.OpenAccess.Metadata.Reflection.Locators;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Telerik.OpenAccess.Metadata;

namespace LINQPad.OpenAccess.Tests.Metadata.Reflection.Locators
{
    /// <summary>
    /// Summary description for AttributesMetadataSourceLocatorFixture
    /// </summary>
    [TestClass]
    public class AttributesMetadataSourceLocatorFixture
    {
        private readonly AttributesMetadataSourceLocator _locator;

        public AttributesMetadataSourceLocatorFixture( )
        {
            this._locator = new AttributesMetadataSourceLocator( new ReflectionService( ) );
        }

        [TestMethod]
        public void LocatesMetadataSourcesFromTestAssembly( )
        {
            var metadataSources = this._locator.GetFromAssembly( Assembly.GetExecutingAssembly( ) ).ToArray( );

            metadataSources.Should( )
                           .NotBeNull( )
                           .And
                           .ContainSingle
                                (
                                    s => s.MappingType == MappingDefinitionType.Attributes
                                            && s.Name == "SofiaCarsRental21Model"
                                            && s.Path == "LINQPad.OpenAccess.Tests.TestModel.Attributes"
                                            && s.Source is AttributesMetadataSource
                                 );
        }
        [TestMethod]
        public void ReturnsEmptyCollectionIfNoSourcesAreFound( )
        {
            var metadataSources = this._locator.GetFromAssembly( typeof( IQueryable ).Assembly );

            metadataSources.Should( )
                           .NotBeNull( )
                           .And
                           .BeEmpty( );
        }
        [TestMethod]
        public void ThrowsArgumentNullExceptionIfAssemblyIsNull( )
        {
            this._locator
                .Invoking( e => e.GetFromAssembly( null ) )
                .ShouldThrow<ArgumentNullException>( );
        }
    }
}